package com.vg.view;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import vg.com.chatonline.activity.R;
import vg.com.parse.JSONParser;
import vg.com.parse.MyAdapter;
import vg.com.parse.Student;
import vg.com.parse.linkUrl;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class edit_infoUser extends com.kds.abs.app.SherlockFragmentActivity {
	EditText etName, etBirthday, etAvatar, etPhone;
	Button btnBrowser, btnOk, btnCancel;
	ImageView imgAvatar;
	Spinner spGender, spCity, spUserType, spReligion, spEducation, spSmoker,
			spDrinker, spMaritalStatus, spBodyType, spColor, spHeight,
			spWeight;
	linkUrl mUrl;
	LinearLayout layout;

	private ArrayList<Student> array;
	MyAdapter adapter;
	JSONParser jparse;
	JSONArray jsa;

	private String prefBody = "PrefBody";
	private String prefCity = "PrefCity";
	private String prefColor = "PrefColor";
	private String prefDrinker = "PrefDrinker";
	private String prefEducation = "PrefEducation";
	private String prefGender = "PrefGender";
	private String prefMaritalStatus = "PrefMaritalStatus";
	private String prefHeight = "PrefHeight";
	private String prefWeight = "PrefWeight";
	private String prefReligion = "PrefReligion";
	private String prefSmoker = "PrefSmoker";
	private String prefUsertype = "PrefUsertype";

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		setContentView(R.layout.activity_edit_infomation);
		imgAvatar = (ImageView) findViewById(R.id.edit_infomation_imgAvarta);
		etName = (EditText) findViewById(R.id.edit_infomation_etName);
		etBirthday = (EditText) findViewById(R.id.edit_infomation_etBirthday);
		etPhone = (EditText) findViewById(R.id.edit_infomation_etPhone);
		btnBrowser = (Button) findViewById(R.id.edit_infomation_btnAvatar);
		btnOk = (Button) findViewById(R.id.edit_infomation_btnOk);
		btnCancel = (Button) findViewById(R.id.edit_infomation_btnCancel);
		spGender = (Spinner) findViewById(R.id.spinner_gender);
		spCity = (Spinner) findViewById(R.id.spinner_city);
		spUserType = (Spinner) findViewById(R.id.spinner_userType);
		spReligion = (Spinner) findViewById(R.id.spinner_religion);
		spEducation = (Spinner) findViewById(R.id.spinner_education);
		spSmoker = (Spinner) findViewById(R.id.spinner_smoker);
		spDrinker = (Spinner) findViewById(R.id.spinner_drinker);
		spMaritalStatus = (Spinner) findViewById(R.id.spinner_maritalStatus);
		spBodyType = (Spinner) findViewById(R.id.spinner_bodyType);
		spColor = (Spinner) findViewById(R.id.spinner_color);
		spHeight = (Spinner) findViewById(R.id.spinner_height);
		spWeight = (Spinner) findViewById(R.id.spinner_weight);
		layout = (LinearLayout) findViewById(R.id.layout_img_avatar);
		DisplayMetrics metrics = this.getResources().getDisplayMetrics();
		int width = metrics.widthPixels;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width / 4,
				width / 4);
		imgAvatar.setLayoutParams(lp);
		mUrl = new linkUrl();

		btnBrowser.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(edit_infoUser.this,
						GridGalleryActivity.class);
				startActivityForResult(i, 1);
			}
		});

		new LoadDataTask().execute();

		dropItems(spBodyType, prefBody, dropSpinner(prefBody));
		dropItems(spCity, prefCity, dropSpinner(prefCity));
		dropItems(spColor, prefColor, dropSpinner(prefColor));
		dropItems(spDrinker, prefDrinker, dropSpinner(prefDrinker));
		dropItems(spEducation, prefEducation, dropSpinner(prefEducation));
		dropItems(spGender, prefGender, dropSpinner(prefGender));
		dropItems(spHeight, prefHeight, dropSpinner(prefHeight));
		dropItems(spMaritalStatus, prefMaritalStatus,
				dropSpinner(prefMaritalStatus));
		dropItems(spReligion, prefReligion, dropSpinner(prefReligion));
		dropItems(spSmoker, prefSmoker, dropSpinner(prefSmoker));
		dropItems(spUserType, prefUsertype, dropSpinner(prefUsertype));
		dropItems(spWeight, prefWeight, dropSpinner(prefWeight));

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				// byte[] result = data.getByteArrayExtra("result");
				// imgAvatar.setImageBitmap(getImage(result));

				String result = data.getStringExtra("result");
				imgAvatar.setImageURI(Uri.parse(result));
			}
			if (resultCode == RESULT_CANCELED) {
			}
		}
	}

	public static Bitmap getImage(byte[] image) {
		return BitmapFactory.decodeByteArray(image, 0, image.length);
	}

	// Luu data tu json, su dung SharedPreferences
	public void putPref(String pref, String url) {
		jparse = new JSONParser();
		jsa = jparse.getJsonarray(url);
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		for (int i = 0; i < jsa.length(); i++) {
			try {
				editor.putString(String.valueOf(i), jsa.getString(i));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		editor.commit();
	}

	// Du du lieu vao Spinner
	public void dropItems(Spinner sp, String pref, ArrayList<Student> arr) {
		array = new ArrayList<Student>();
		adapter = new MyAdapter(this, R.layout.drop_gender, arr, true);
		sp.setAdapter(adapter);
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for (int i = 0; i < prefs.getAll().size(); i++) {
			Student n = new Student(prefs.getString(String.valueOf(i), ""));
			array.add(n);
		}
		adapter.notifyDataSetChanged();
	}

	public ArrayList<Student> dropSpinner(String pref) {
		ArrayList<Student> arrayList = new ArrayList<Student>();
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for (int i = 0; i < prefs.getAll().size(); i++) {
			Student n = new Student(prefs.getString(String.valueOf(i), ""));
			arrayList.add(n);
		}
		return arrayList;
	}

	// Thuc hien load du lieu ngam
	private class LoadDataTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... v) {
			putPref(prefBody, mUrl.body_type);
			putPref(prefCity, mUrl.city);
			putPref(prefColor, mUrl.color);
			putPref(prefDrinker, mUrl.drinker);
			putPref(prefEducation, mUrl.education);
			putPref(prefGender, mUrl.gender);
			putPref(prefHeight, mUrl.my_height);
			putPref(prefMaritalStatus, mUrl.marital_status);
			putPref(prefReligion, mUrl.religion);
			putPref(prefSmoker, mUrl.smoker);
			putPref(prefUsertype, mUrl.user_type);
			putPref(prefWeight, mUrl.my_weight);
			return null;
		}

		@Override
		protected void onPostExecute(Void v) {
		}
	}
}
